package com.crm.controller;


import com.crm.dto.ActivityDTO;
import com.crm.dto.AddUpActivityDTO;

import com.crm.pojo.Activity;
import com.crm.result.PageResult;
import com.crm.result.Result;
import com.crm.result.Result_code;
import com.crm.service.ActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/clues")
@Api(tags = "活动管理接口")
public class ActivityController {

    @Autowired
    private ActivityService activityService;




    @ApiOperation("分页查询")
    @GetMapping("/activity/list")
    public Result_code getActivity( String code,Integer channel,
                                    @RequestParam(value = "params[beginCreateTime]", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginCreateTime,
                                    @RequestParam(value = "params[endCreateTime]", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endCreateTime,
                                    @RequestParam(value = "params[beginTime]", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginTime,
                                    @RequestParam(value = "params[endTime]", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime) {
        ActivityDTO dto = new ActivityDTO();
        dto.setCode(code);
        dto.setChannel(channel);
        dto.setBeginTime(beginTime);
        dto.setEndTime(endTime);
        dto.setBeginCreateTime(beginCreateTime);
        dto.setEndCreateTime(endCreateTime);



        PageResult activity = activityService.getActivity(dto);
               // 组装统计信息
        Map<String, Object> params = new HashMap<>();
        params.put("total", 0);
        params.put("pass", 0);
        params.put("reject", 0);
        params.put("toAudit", 0);
        params.put("finish", 0);

        log.info("返回的最终活动数据{}",activity);
        return Result_code.commonSuccess(activity,params);
    }


    @ApiOperation("添加活动")
    @PostMapping("/activity")
    public Result addActivity(@RequestBody AddUpActivityDTO dto) {
        activityService.addActivity(dto);
        return Result.success();
    }



    @ApiOperation("根据id查询活动-ApiFox已测")
    @GetMapping("activity/{id}")
    public Result_code getByid(@PathVariable Integer id){
        log.info("根据id{}查询活动",id);
    Activity activity=activityService.getByid(id);
        return Result_code.success(activity);
    }


    @ApiOperation("更新活动")
    @PutMapping("/activity")
    public Result_code update(@RequestBody AddUpActivityDTO dto){
        log.info("更新活动接到的请求参数: {}", dto);
        Result_code result =activityService.update(dto);
        return result;

    }


    @ApiOperation("删除活动")
    @DeleteMapping("/activity/{id}")
    public Result_code delete(@PathVariable Integer id){
        log.info("删除请求id是{}",id);
        return  activityService.delete(id);
    }

    /**
     * 查询所有的活动信息
     */
    @GetMapping("/activity/listselect/{channel}")
    public Result queryList(@PathVariable String channel){
        log.info("根据id查询所有活动信息");
        List<Activity>  activity=activityService.queryList(channel);
        return Result.success(activity);
    }
}
